/**
 * @(#) AddressProxy.as
 *
 * @date		2009. 02. 18.
 * @version		1.0
 * @author		Shin, Yunwoo
 * @description	 주소변경 - Proxy
 * 				
 */
package com.skt.common_hr.myprofile_sac.model
{
	import com.skt.common_hr.common.puremvc.CommonProxy;
	import com.skt.common_hr.myprofile_sac.model.vo.ScholarshipVO;
	import com.wcl.utils.DateUtil;
	
	import mx.collections.ArrayCollection;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	
	public class ScholarshipProxy extends CommonProxy
	{
		public static const NAME:String = "ScholarshipProxy";
		
		public static const LOAD_SCHOLARSHIP_COMPLETE:String 	= "loadScholarshipComplete";
		public static const LOAD_SCHOLARSHIP_FAILED:String 		= "loadScholarshipFaileds";
		public static const SAVE_SCHOLARSHIP_COMPLETE:String 	= "saveScholarshipComplete";
		public static const SAVE_SCHOLARSHIP_FAILED:String 		= "saveScholarshipFailed";

		public var scholarshipList:ArrayCollection;
		public var schoolLevel:ArrayCollection;
		public var codeList2:ArrayCollection;
		
		/**
		 * 생성자
		 * 
		 * @param
		 * @return void
		 */
		public function ScholarshipProxy(data:Object=null)
		{
			super(NAME, data);
		}
		
		
		
		
		public function load(pernr:String):void
		{
			var params:Object = new Object();
			params["I_PERNR"] = pernr;
			params["I_ACTIO"] = "DIS";
						
			loadData("Z_HR_0119",params, result, fault); 
		}
		

		public function save(pernr:String, arr:Array, sI_NO:String):void
		{
			var params:Object = new Object();
			params["I_PERNR"] = pernr;
			params["I_ACTIO"] = "INS";
			params["I_NO"] = sI_NO;
			
			var table:Object = new Object();
			table["T_IN"] = arr;
			
			saveData("Z_HR_0119", params, table, resultSave, faultSave); 
		}


		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Event Listeners
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 * 
		 * @param
		 * @return void
		 */
		public var sE_NO:String;
		public function result(event:ResultEvent):void
		{
			if(retCode==0) {
				scholarshipList = parseScholarshipList(event.result.T_OUT);
				schoolLevel = parseSchoolLevel(event.result.T_CODE1);
				codeList2 = parseCodeList(event.result.T_CODE2);
				sE_NO = event.result.E_NO;
				facade.sendNotification(LOAD_SCHOLARSHIP_COMPLETE, this);
			}
			else {
				facade.sendNotification(LOAD_SCHOLARSHIP_FAILED, this);
			}
		}
		
		/**
		 * 서버에서 오류가 발생했을 처리
		 * 
		 * @param
		 * @return void
		 */
		private function fault(event:FaultEvent):void
		{
			facade.sendNotification(LOAD_SCHOLARSHIP_FAILED, this);
		}
		
		
		
		public function resultSave(event:ResultEvent):void
		{
			if(retCode==0) {
				facade.sendNotification(SAVE_SCHOLARSHIP_COMPLETE, this);
			}
			else {
				facade.sendNotification(SAVE_SCHOLARSHIP_FAILED, this);
			}
		}
		
		private function faultSave(event:FaultEvent):void
		{
			facade.sendNotification(SAVE_SCHOLARSHIP_FAILED, this);
		}
		
		

		private function parseScholarshipList(src:ArrayCollection):ArrayCollection
		{
			var scholarshipList:ArrayCollection = new ArrayCollection();
			for each (var item:Object in src)
			{
				var vo:ScholarshipVO = new ScholarshipVO;
				
				vo.STEXT	=	item.STEXT;				//교육 기관 텍스트
				vo.BEGDA	=	DateUtil.stringToDateTime(item.BEGDA);	//발령 유형 이름
				vo.ENDDA	=	DateUtil.stringToDateTime(item.ENDDA);	//발령 사유 이름
				
				vo.SCHCDT	= 	item.SCHCDT;	//학교
				vo.SLTPTX	=	item.SLTPTX;	//전공텍스트
				vo.LOCAT	=	item.LOCAT;		//소재지
				vo.ENTCH	=	item.ENTCH;		//입사 당시 학력
				vo.SLABS	=	item.SLABS;		//학위
				vo.SLABST	=	item.SLABST;	//증명서 텍스트
				vo.SLTP2	=	item.SLTP2;		//전공
				vo.SLTP2T	=	item.SLTP2T;	//전공텍스트
				vo.SLTP1	=	item.SLTP1;		//전공
				vo.INSTI	=	item.INSTI;		//교육기관/위치
				vo.SLART	=	item.SLART;		//교육기관
				vo.SUBTY	=	item.SUBTY;		// 
				vo.SEQNR	=	item.SEQNR;		// 
				
				scholarshipList.addItem(vo);
			}
			return scholarshipList;
		}
		
		private function parseSchoolLevel(src:ArrayCollection):ArrayCollection
		{
			var levels:ArrayCollection = new ArrayCollection();
			for each(var item:Object in src) {
				var vo:Object = new Object();
				vo.label = item.TEXT;
				vo.data = item.KEY;
				levels.addItem(vo);
			}
			return levels;
		}

		private function parseCodeList(src:ArrayCollection):ArrayCollection
		{
			var list:ArrayCollection = new ArrayCollection();
			for each(var item:Object in src) {
				var vo:Object = new Object();
				vo.label = item.TEXT;
				vo.data = item.KEY;
				list.addItem(vo);
			}
			return list;
		}

		public function toJSONobject(vo:ScholarshipVO):Object
		{
			var jsonObj:Object = new Object();
				
				jsonObj["STEXT"]	=	vo.STEXT;	//BEGDA
				jsonObj["BEGDA"]	=	DateUtil.toBasicString(vo.BEGDA);	//발령 유형 이름
				jsonObj["ENDDA"]	=	DateUtil.toBasicString(vo.ENDDA);	//발령 사유 이름
				jsonObj["SCHCDT"]	= 	vo.SCHCDT;						//학교
				jsonObj["SLTPTX"]	= 	vo.SLTPTX;						//전공텍스트
				jsonObj["LOCAT"]	= 	vo.LOCAT;						//소재지
				jsonObj["ENTCH"]	= 	vo.ENTCH;	//입사 당시 학력
				jsonObj["SLABS"]	= 	vo.SLABS;	//학위
				jsonObj["SLABST"]	= 	vo.SLABST;	//증명서 텍스트
				jsonObj["SLTP2"]	= 	vo.SLTP2;	//부서명(상위조직명 포함)
				jsonObj["SLTP2T"]	= 	vo.SLTP2T;	//부서명(상위조직명 포함)
				jsonObj["SLTP1"]	= 	vo.SLTP1;	//부서명(상위조직명 포함)
				jsonObj["INSTI"]	= 	vo.INSTI;	//부서명(상위조직명 포함)
				jsonObj["SLART"]	= 	vo.SLART;	//부서명(상위조직명 포함)
				jsonObj["SUBTY"]	= 	vo.SUBTY;	//부서명(상위조직명 포함)
				jsonObj["SEQNR"]	= 	vo.SEQNR;	//부서명(상위조직명 포함)
			return jsonObj;
		}
	}
}